package com.moo.imgprogress.imgprogressbar;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * author：moodstrikerdd
 * date：2017/3/22
 * introduction：
 */
public class ImgProgressBar extends View {
    private Bitmap startBitmap;
    private Bitmap endBitmap;

    private int progressColor;
    private int progressBg;

    private int progressWidth;

    private int mWidth;
    private int mHeight;

    private Paint mPaint;
    private Rect mRect;

    private int progress;
    private int maxProgress;

    private int offset = 0;
    private int padding = 30;


    public ImgProgressBar(Context context) {
        this(context, null);
    }

    public ImgProgressBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ImgProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    //初始化attrs
    private void init() {
        maxProgress = 100;
        progress = 40;
        progressWidth = 20;

        startBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.a);
        endBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.a);

        progressBg = getResources().getColor(R.color.colorProgressBg);
        progressColor = getResources().getColor(R.color.colorProgress);

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(progressWidth);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        int specSize = MeasureSpec.getSize(widthMeasureSpec);

        if (specMode == MeasureSpec.EXACTLY)// match_parent , accurate
        {
            mWidth = specSize;
        } else {
            int desire = 300;

            if (specMode == MeasureSpec.AT_MOST)// wrap_content
            {
                mWidth = Math.min(desire, specSize);
            }
        }

        specMode = MeasureSpec.getMode(heightMeasureSpec);
        specSize = MeasureSpec.getSize(heightMeasureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            mHeight = specSize;
        } else {
            int desire = 300;
            if (specMode == MeasureSpec.AT_MOST)// wrap_content
            {
                mHeight = Math.min(desire, specSize);
            }
        }
        //为控件指定大小
        mWidth = Math.min(mWidth, mHeight);
        setMeasuredDimension(mWidth, mWidth);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawCircle(canvas);
        drawBitmap(canvas);
    }

    //绘制图形
    private void drawBitmap(Canvas canvas) {
        mRect = new Rect(mWidth / 2 - progressWidth / 2 - offset, +padding - offset, mWidth / 2 + progressWidth / 2 + offset, progressWidth + offset + padding);
        canvas.drawBitmap(startBitmap, null, mRect, mPaint);

        double angle = (2 * Math.PI / 360) * (360.00 * (double) progress / (double) maxProgress);
        int centerX = (int) (mWidth / 2 + (mWidth / 2 - progressWidth / 2 - padding) * Math.sin(angle));
        int centerY = (int) (mWidth / 2 - (mWidth / 2 - progressWidth / 2 - padding) * Math.cos(angle));

        mPaint.setStrokeWidth(10);
        mPaint.setColor(progressBg);
        mRect = new Rect(centerX - progressWidth / 2 - offset, centerY - progressWidth / 2 - offset, centerX + progressWidth / 2 + offset, centerY + progressWidth / 2 + offset);
        canvas.drawBitmap(startBitmap, null, mRect, mPaint);
    }

    //绘制进度
    private void drawCircle(Canvas canvas) {
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(progressWidth);
        mPaint.setColor(progressBg);
        canvas.drawArc(new RectF(padding + progressWidth / 2, padding + progressWidth / 2, mWidth - progressWidth / 2 - padding, mWidth - progressWidth / 2 - padding), 0, 360, false, mPaint);

        mPaint.setColor(progressColor);
        canvas.drawArc(new RectF(padding + progressWidth / 2, padding + progressWidth / 2, mWidth - progressWidth / 2 - padding, mWidth - progressWidth / 2 - padding), -90, 360 * progress / maxProgress, false, mPaint);
    }


    public synchronized void setProgress(int progress) {
        this.progress = progress;
        postInvalidate();
    }

    public synchronized void setAnimationProgress(final int progress) {
        setProgress(0);
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < progress; i++) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    setProgress(i + 1);
                }
            }
        }).start();
    }

    public void setOffset(int offset) {
        this.offset = offset;
    }
}
